package gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Menu extends JFrame {

	private static final String FILE = "./MonteCarlo.csv";
	public static final String TITLE = "Monte Carlo Simulation";
	public static final String SET1 = "Launch 1st set of arguments";
	public static final String SET2 = "Launch 2nd set of arguments";
	public static final String SET3 = "Launch 3rd set of arguments";
	public static final String SET4 = "Launch 4th set of arguments";
	public static final String SET5 = "Launch 5th set of arguments";

	private JButton set1;
	private JButton set2;
	private JButton set3;
	private JButton set4;
	private JButton set5;
	private JLabel lblresult;
	private JLabel lbltime;
	private boolean ready;
	
	double S=0;
	double X=0;
	double T=0;
	double b=0;
	double v=0;
	int nSteps=0;
	int nSimulations=0;



	/**
	 * Constructor
	 */
	public Menu() {
		this.setSize(300, 500);
		this.setTitle(TITLE);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		JPanel pan = new JPanel();
		init(pan);
		this.setContentPane(pan);
		this.setVisible(true);
	}

	/**
	 * initialize graphic components
	 * 
	 * @param pan
	 */
	private void init(JPanel pan) {

		lblresult = new JLabel("result : ");
		ready = false;
		lblresult.setPreferredSize(new Dimension(250, 30));
		lbltime = new JLabel("time : ");
		lbltime.setPreferredSize(new Dimension(250, 30));
		set1 = new JButton(SET1);
		set2 = new JButton(SET2);
		set3 = new JButton(SET3);
		set4 = new JButton(SET4);
		set5 = new JButton(SET5);
		
		set1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					BufferedReader br = new BufferedReader(new FileReader(FILE));
					String line = "";
					String separator = ";";
					line = br.readLine();
					String[] args = line.split(separator);
					S = Double.parseDouble(args[0]);
					X = Double.parseDouble(args[1]);
					r = Double.parseDouble(args[2]);
					T = Double.parseDouble(args[3]);
					b = Double.parseDouble(args[4]);
					v = Double.parseDouble(args[5]);
					nSteps = Integer.parseInt(args[6]);
					nSimulations = Integer.parseInt(args[7]);
					br.close();
					ready = true;
				} catch (FileNotFoundException e) {
				} catch (IOException e) {
				}
			}
		});

		set2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					BufferedReader br = new BufferedReader(new FileReader(FILE));
					String line = "";
					String separator = ";";
					line = br.readLine();
					line = br.readLine();
					String[] args = line.split(separator);
					S = Double.parseDouble(args[0]);
					X = Double.parseDouble(args[1]);
					r = Double.parseDouble(args[2]);
					T = Double.parseDouble(args[3]);
					b = Double.parseDouble(args[4]);
					v = Double.parseDouble(args[5]);
					nSteps = Integer.parseInt(args[6]);
					nSimulations = Integer.parseInt(args[7]);
					br.close();
					ready = true;
				} catch (FileNotFoundException e) {
				} catch (IOException e) {
				}
			}
		});

		set3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					BufferedReader br = new BufferedReader(new FileReader(FILE));
					String line = "";
					String separator = ";";
					line = br.readLine();
					line = br.readLine();
					line = br.readLine();
					String[] args = line.split(separator);
					S = Double.parseDouble(args[0]);
					X = Double.parseDouble(args[1]);
					r = Double.parseDouble(args[2]);
					T = Double.parseDouble(args[3]);
					b = Double.parseDouble(args[4]);
					v = Double.parseDouble(args[5]);
					nSteps = Integer.parseInt(args[6]);
					nSimulations = Integer.parseInt(args[7]);
					br.close();
					ready = true;
				} catch (FileNotFoundException e) {
				} catch (IOException e) {
				}
			}
		});

		set4.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					BufferedReader br = new BufferedReader(new FileReader(FILE));
					String line = "";
					String separator = ";";
					line = br.readLine();
					line = br.readLine();
					line = br.readLine();
					line = br.readLine();
					String[] args = line.split(separator);
					S = Double.parseDouble(args[0]);
					X = Double.parseDouble(args[1]);
					r = Double.parseDouble(args[2]);
					T = Double.parseDouble(args[3]);
					b = Double.parseDouble(args[4]);
					v = Double.parseDouble(args[5]);
					nSteps = Integer.parseInt(args[6]);
					nSimulations = Integer.parseInt(args[7]);
					br.close();
					ready = true;
				} catch (FileNotFoundException e) {
				} catch (IOException e) {
				}
			}
		});

		set5.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					BufferedReader br = new BufferedReader(new FileReader(FILE));
					String line = "";
					String separator = ";";
					line = br.readLine();
					line = br.readLine();
					line = br.readLine();
					line = br.readLine();
					line = br.readLine();
					String[] args = line.split(separator);
					S = Double.parseDouble(args[0]);
					X = Double.parseDouble(args[1]);
					r = Double.parseDouble(args[2]);
					T = Double.parseDouble(args[3]);
					b = Double.parseDouble(args[4]);
					v = Double.parseDouble(args[5]);
					nSteps = Integer.parseInt(args[6]);
					nSimulations = Integer.parseInt(args[7]);
					br.close();
					ready = true;
				} catch (FileNotFoundException e) {
				} catch (IOException e) {
				}
			}
		});

		pan.add(set1);
		pan.add(set2);
		pan.add(set3);
		pan.add(set4);
		pan.add(set5);
		pan.add(lblresult);
		pan.add(lbltime);
	}
	
	double r;
	public double getR() {
		return r;
	}

	public double getS() {
		return S;
	}

	public double getvalueX() {
		return X;
	}

	public double getT() {
		return T;
	}

	public double getB() {
		return b;
	}

	public double getV() {
		return v;
	}

	public int getnSteps() {
		return nSteps;
	}

	public int getnSimulations() {
		return nSimulations;
	}

	public JLabel getLblresult() {
		return lblresult;
	}

	public JLabel getTime() {
		return lbltime;
	}

	public boolean isReady() {
		// TODO Auto-generated method stub
		return ready;
	}

	public void setReady(boolean c) {
		// TODO Auto-generated method stub
		ready = c;
	}
}
